/********************************************
 * 功能说明: Web端Message基础业务控制器
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员: Frank
 * 开发时间: 2019-10-17 18:02:38
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.message.controller;

import com.aliyuncs.exceptions.ClientException;
import com.codahale.metrics.annotation.Timed;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.message.converter.MessageConverter;
import com.spring.arch.message.repository.MessageRepository;
import com.spring.arch.message.service.MessageService;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.message.converter.MessageConverter;
import com.spring.arch.message.dto.MessageDTO;
import com.spring.arch.message.model.Message;
import com.spring.arch.message.repository.MessageRepository;
import com.spring.arch.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * Web端Message基础业务控制器
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019-10-17 18:02:38
 */
@Slf4j
@RestController
@RequestMapping("/message/msg")
@Api(tags = {"Message管理API"})
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private MessageRepository messageRepository;
    @Autowired
    private MessageConverter messageConverter;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @Timed
    @ApiOperation(value = "获取分页数据")
    @PostMapping(value = "/s", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResultDTO<MessageDTO> search(final Pageable pageable) {
        final Page<Message> models = this.messageRepository.findAll(pageable);
        return this.messageConverter.toResultDTO(models);
    }

    /**
     * 工单接单提醒
     *
     * @return 执行结果
     */
    @Timed
    @ApiOperation(value = "工单接单提醒")
    @PostMapping(value = "/workorder/receive")
    public ResultDTO receiveWorkorder(@RequestParam(value = "orderType") String orderType,
                                      @RequestParam(value = "phone") String phone,
                                      @RequestParam(value = "orderCode") String orderCode) throws ClientException {

        boolean result = messageService.receptWorkorder(orderType, orderCode, phone);

        if(result) {
            return ResultDTO.success();
        } else {
            return ResultDTO.failure();
        }
    }

    /**
     * 工单超时提醒
     *
     * @return 执行结果
     */
    @Timed
    @ApiOperation(value = "工单超时提醒")
    @PostMapping(value = "/workorder/timeout")
    public ResultDTO timeoutWorkorder(@RequestParam(value = "orderType") String orderType,
                                     @RequestParam(value = "phone") String phone,
                                       @RequestParam(value = "orderCode") String orderCode,
                                       @RequestParam(value = "orderProcess") String orderProcess) throws ClientException {

        boolean result = messageService.overtimeWorkorder(orderType, orderCode, orderProcess, phone);

        if(result) {
            return ResultDTO.success();
        } else {
            return ResultDTO.failure();
        }
    }

    /**
     * 工单接单提醒
     *
     * @return 执行结果
     */
    @Timed
    @ApiOperation(value = "工单关注提醒")
    @PostMapping(value = "/workorder/follow")
    public ResultDTO followWorkorder(@RequestParam(value = "orderType") String orderType,
                                     @RequestParam(value = "phone") String phone,
                                     @RequestParam(value = "orderCode") String orderCode) throws ClientException {

        boolean result = messageService.followWorkorder(orderType, orderCode, phone);

        if(result) {
            return ResultDTO.success();
        } else {
            return ResultDTO.failure();
        }
    }
}
